package cn.edu.gzhmt.cart.web.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.gzhmt.cart.domain.Cart;
import cn.edu.gzhmt.cart.domain.CartItem;
import cn.edu.gzhmt.goods.domain.Goods;
import cn.edu.gzhmt.goods.service.IGoodsService;
import cn.edu.gzhmt.goods.service.impl.GoodsServiceImpl;
import cn.edu.gzhmt.utils.web.servlet.BaseServlet;

public class CartServlet extends BaseServlet {

	private IGoodsService goodsService = new GoodsServiceImpl();

	/**
	 * 加入购物车:只是把商品添加到购物车中，弹出加入购物车成功信息，不进行页面跳转
	 * 异步
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String addInCart(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		// 判断是否有购物车，没有说明还未登录
		Cart cart = (Cart) request.getSession().getAttribute("session_cart");
		if (cart == null) {

			return null;
		}

		String gid = request.getParameter("gid");
		int count = 0;
		try {
			count = Integer.parseInt(request.getParameter("count"));
		} catch (NumberFormatException e) {
			count = 1;
		}

		// 查询商品
		Goods goods = goodsService.loadGoods(gid);
		if (goods == null) {
			return null;
		}

		// 把商品添加到购物车中
		CartItem cartItem = new CartItem(goods, count);
		cart.addCartItem(cartItem);
		request.getSession().setAttribute("session_cart", cart);

		response.getWriter().print("加入购物车成功");

		return null;
	}

	/**
	 * 立即购买
	 * 把商品加入购物车，并跳转到购物车页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String buyNow(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		// 判断是否有购物车，没有说明还未登录
		Cart cart = (Cart) request.getSession().getAttribute("session_cart");
		if (cart == null) {
			request.setAttribute("msg", "您还未登录，请在登录后进行购买！");
			return "f:/jsps/users/login.jsp";
		}

		// 获取参数
		String gid = request.getParameter("gid");
		int count = 0;
		try {
			count = Integer.parseInt(request.getParameter("count"));
		} catch (NumberFormatException e) {
			count = 1;
		}

		// 把商品添加到购物车中
		Goods goods = goodsService.loadGoods(gid);
		if (goods == null) {
			return null;
		}
		cart.addCartItem(new CartItem(goods, count));
		request.getSession().setAttribute("session_cart", cart);
		
		return "f:/jsps/cart/shoppingCar.jsp";
	}
	
	
	/**
	 * 根据gid删除购物车中的商品
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String removeItem(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		
		Cart cart = (Cart) request.getSession().getAttribute("session_cart");
		if (cart == null) {
			request.setAttribute("msg", "您还未登录，请在登录后进行购买！");
			return "f:/jsps/users/login.jsp";
		}
		
		// 获取gid进行删除
		String gid = request.getParameter("gid");
		
		cart.remove(gid);
		request.getSession().setAttribute("session_cart", cart);
		
		response.getWriter().print("true");

		return null;
	}
	
	
	/**
	 * 清空购物车
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public String cleanCart(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		Cart cart = (Cart) request.getSession().getAttribute("session_cart");
		if (cart == null) {
			request.setAttribute("msg", "您还未登录，不要瞎搞！！！");
			return "f:/jsps/users/login.jsp";
		}
		
		cart.cleanCart();
		
		return "r:/jsps/cart/shoppingCar.jsp";
	}
}
